package cn.bytecollege.admin.controller;

import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.Merchant;
import cn.bytecollege.common.entity.Page;
import cn.bytecollege.common.entity.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 商家搜索自己的商品
 */
@WebServlet(name = "ProductListOfMerchantController",value = "/admin/productListMerchant")
public class ProductListOfMerchantController extends HttpServlet {
    private ProductService productService = null;
    public ProductListOfMerchantController(){
        productService = new ProductService();
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Merchant merchant = (Merchant) session.getAttribute("merchant");
        int merchantId = merchant.getMerchantId();
        //前台给我们传过来的参数是index
        String index = request.getParameter("index");
        int pageIndex = (index==null)?1:Integer.parseInt(index);
        String size = request.getParameter("size");
        //一面显示5页。
        int pageSize = (size==null)?5:Integer.parseInt(size);

        Page<Product> page = productService.findListByMerchantId(merchantId,pageIndex,pageSize);
        request.setAttribute("page",page);
        request.getRequestDispatcher("/admin/html/device/productList.jsp").forward(request,response);

    }
}
